import request from '~/utils/request'

export function getShoppingCart(uid) {
  return request({
    url: `/goods/shopping/trolley/getGoods/${uid}`,
    method: 'get'
  })
}

export function addOneGoods(data) {
  return request({
    url: '/goods/shopping/trolley/addGood',
    method: 'post',
    data
  })
}

// 将临时购物车添加进用户购物车
export function addTempCartToCart(uid, goodsList) {
  const goods = []
  goodsList.forEach(element => {
    goods.push({
      uid,
      qty: element.count,
      gid: parseInt(element.gid)
    })
  })
  return request({
    url: '/goods/shopping/trolley/addGoodBatch',
    method: 'post',
    data: {
      goods: goods
    }
  })
}

export function editShoppingCart(gid, count, uid) {
  return request({
    url: '/goods/shopping/trolley/editQty',
    method: 'put',
    data: {
      gid,
      qty: count,
      uid
    }
  })
}

export function deleteShoppingCart(goodsIdList, uid) {
  return request({
    url: '/goods/shopping/trolley/removeGood',
    method: 'delete',
    data: {
      gids: goodsIdList,
      uid
    }
  })
}

// 通过临时购物车的商品id查询信息
export function getShoppingCartByTempCart(idList) {
  return request({
    url: '/goods/search/getGoodsByGids',
    method: 'post',
    data: {
      gids: idList
    }
  })
}

// 批量查询商品数据
export function getGoodsBatch(uid, idList) {
  return request({
    url: '/goods/shopping/trolley/getGoodsBatch',
    method: 'post',
    data: {
      gids: idList,
      uid
    }
  })
}
